﻿using System;
using System.Text;
using System.IO;


class Log
{
    //The file stream is used in all methods, so it is public
    private static StreamWriter LogFile;

    //For debug messagse

    public enum LogLevel
    {
        System,
        Verbatim,
        Normal,
        Warning,
        Error
    }

    //----------------------------------------------------------------------
    //This starts up the logging system
    public static void InitializeLogging()
    {
        bool newfile = File.Exists("sessions.log");
        LogFile = File.AppendText("sessions.log");
        LogFile.AutoFlush = true;

        if(newfile) LogFile.WriteLine("\n\n\n");
        Write("NEW SESSION: " + DateTime.Now.ToLongDateString(), LogLevel.System);
    }

    //----------------------------------------------------------------------
    //This writes a line into the log
    public static void Write(string text, LogLevel level, bool printToConsole = true)
    {
        //Set message level
        switch (level)
        {
            case LogLevel.Verbatim: 
                Console.ForegroundColor = ConsoleColor.Blue;
                break;

            case LogLevel.Error: 
                Console.ForegroundColor = ConsoleColor.Red; 
                break;

            case LogLevel.Warning: 
                Console.ForegroundColor = ConsoleColor.Yellow; 
                break;

            default: 
                Console.ForegroundColor = ConsoleColor.Green; 
                break;
        }

        if (printToConsole)
        {
            Console.WriteLine(text);
        }
        Console.ForegroundColor = ConsoleColor.White;

        //Error messages need confirmation to continue
        if (level == LogLevel.Error) Console.ReadKey();

        LogFile.WriteLine("{0}: {1}", DateTime.Now.ToLongTimeString(), text);
    }

    //----------------------------------------------------------------------
    //This releases all resources the logger uses
    public static void CloseLog()
    {
        LogFile.Write("{0}: CloseLog() -  ", DateTime.Now.ToLongTimeString());
        LogFile.WriteLine("-----END OF SESSION-----");
        LogFile.Close();
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Shutting down session log...");
        LogFile.AutoFlush = false;
    
    }
}


